package controller;

import Pojo.Apply;
import Pojo.Checkout;
import Pojo.Hetong;
import Pojo.Houselist;
import Pojo.Zulist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.ApplyService;
import service.CheckoutService;
import service.HetongService;
import service.HouselistService;
import service.ZulistService;

@Controller
@RequestMapping({"/hetong"})
public class HetongController
{

  @Autowired
  private HetongService hetongService;

  @Autowired
  private HouselistService houselistService;

  @Autowired
  private ApplyService applyService;

  @Autowired
  private ZulistService zulistService;

  @Autowired
  private CheckoutService checkoutService;

  @RequestMapping({"/inserthetong"})
  public String inserthetong(Model model, Hetong hetong)
  {
    this.hetongService.inserthetong(hetong);
    Hetong hetong1 = this.hetongService.findhetong(hetong.getHouse_id());

    Houselist houselist = this.houselistService.findhouseid(hetong1.getHouse_id());
    houselist.setStatus("已租赁");
    this.houselistService.updatehousestatus(houselist);

    Zulist zulist = new Zulist();
    Apply apply = this.applyService.findbyhouse_id(hetong.getHouse_id());
    zulist.setHouse_id(hetong.getHouse_id());
    zulist.setUserlist_id(apply.getUserlist_id());
    zulist.setContract_id(hetong1.getId());
    zulist.setPrice(Double.valueOf(apply.getPrice()));
    zulist.setAddress(apply.getAddress());
    this.zulistService.insertzulist(zulist);

    this.applyService.deletebyhouse_id(hetong1.getHouse_id());
    model.addAttribute("error", "zusuccess");
    return "redirect:/zulist/findzulist.action";
  }

  @RequestMapping({"/seehetong"})
  public String seehetong(String house_id, Model model) {
    Hetong hetong = this.hetongService.findhetong(house_id);
    model.addAttribute("hetong", hetong);
    model.addAttribute("mainPage", "hetong.jsp");
    return "admin/main1"; }

  @RequestMapping({"/updatehetong"})
  public String updatehetong(String house_id, Model model) {
    Hetong hetong = this.hetongService.findhetong(house_id);
    model.addAttribute("hetong", hetong);
    model.addAttribute("mainPage", "updatehetong.jsp");
    return "admin/main1"; }

  @RequestMapping({"/changehetong"})
  public String changehetong(Hetong hetong) {
    this.hetongService.updatehetong(hetong);

    return "redirect:/zulist/findzulist.action";
  }

  @RequestMapping({"/deletehetong"})
  public String deletehetong(String house_id, Model model)
  {
    this.hetongService.deletehetong(house_id);
    Zulist zulist = this.zulistService.findzulist(house_id);
    Checkout checkout = new Checkout();
    checkout.setHouse_id(house_id);
    checkout.setAddress(zulist.getAddress());
    checkout.setStatus("已退租");
    checkout.setUserlist_id(zulist.getUserlist_id());
    this.checkoutService.insertcheckout(checkout);
    this.houselistService.deletehousebyhouseid(house_id);
    this.zulistService.deletezulist(house_id);

    model.addAttribute("error", "checkoutsuccess");
    return "redirect:/zulist/findzulist.action";
  }

  @RequestMapping({"/zukeseehetong"})
  public String zukeseehetong(String house_id, Model model) {
    Hetong hetong = this.hetongService.findhetong(house_id);
    model.addAttribute("hetong", hetong);
    model.addAttribute("mainPage", "showhetong.jsp");
    return "zuke/main";
  }
}